layui.use(['table', 'jquery'], function () {
    const table = layui.table,
        $ = layui.jquery;

    //显示日期
    showTime('#startDate');
    showTime('#endDate');

    let loginUserCode = localStorage.getItem("loginUserCode");
    if(loginUserCode==='admin'){
        loginUserCode='';
    }
    //默认加载时执行
    findAll();

    function findAll() {
        // let userCode = $('#userCode').val();
        let username = $('#username').val();
        let startDate = $('#startDate').val();
        let endDate = $('#endDate').val();
        let params = {
            userCode: loginUserCode,
            username: username,
        };
        if (startDate !== undefined && startDate != null && startDate !== '') {
            params['startDate'] = startDate
        }
        if (endDate !== undefined && endDate != null && endDate !== '') {
            params['endDate'] = endDate
        }

        let cols = [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', width: 80, title: 'ID', sort: true, fixed: 'left'}
            , {field: 'userCode', width: '15%', title: '用户编号'}
            , {field: 'username', width: '25%', title: '用户名'}
            , {field: 'loginTime', width: '25%', title: '登录时间', sort: true}
            , {field: 'ip', width: '25%', title: '登录IP地址'}
        ]];

        table.render({
            elem: '#dataList'
            , url: LOCAL_CROSS_URL + "/userLoginRecord/findAll"
            , where: params
            , method: 'get'
            , request: REQUEST
            , headers: HEADERS
            , response: RESPONSE
            , cellMinWidth: 80
            , height: 720
            , toolbar: '#toolbar'
            , skin: 'line'
            , even: true
            , page: true
            , limits: [5, 10, 20, 30, 50]
            , limit: 10
            , title: '用户登录日志表'
            , cols: cols
        });
    }

    //监听头工具栏事件
    table.on('toolbar(dataList)', function (obj) {
        const checkStatus = table.checkStatus(obj.config.id)
            , data = checkStatus.data; //获取选中的数据
        switch (obj.event) {
            case 'batchDel':
                if (data.length === 0) {
                    layer.msg('请至少选择一行');
                } else {
                    let ids = [];
                    for (let i = 0; i < data.length; i++) {
                        ids += checkStatus.data[i].id + ',';
                    }
                    //去掉最后一个逗号
                    ids = ids.substring(0, ids.lastIndexOf(','));
                    layer.confirm('真的删除行么', function (index) {
                        layer.close(index);
                        batchDel(ids.toString())
                    });
                }
                break;
        }
    });

    /**
     * 批量删除
     */
    function batchDel(ids) {

        let result = commonBatchAjax('/userLoginRecord/deletes', {ids: ids}, 'post');
        if (result.code === 200) {
            layerMsgDeleteSuccess();
            findAll();
        } else {
            layerMsgDeleteFail();
        }
    }

    /**
     * 查询
     */
    $('#query').on('click', function () {
        findAll()
    });

    /**
     * 重置
     */
    $('#reset').on('click', function () {
        $('#username').val('');
        $('#startDate').val('');
        $('#endDate').val('');
    });
});